import win32com.client
import os
import time
import pyautogui

class Running_12:
    def __init__(self):
        self.dm = win32com.client.Dispatch("dm.dmsoft")
        print("大漠插件版本：", self.dm.Ver())
        script_dir = os.path.dirname(os.path.abspath(__file__))
        print("当前目录", script_dir)
        self.dm.SetPath(os.path.join(script_dir))

        # 关键修改1：强制设置绝对路径
        self.script_dir = os.path.dirname(os.path.abspath(__file__))
        self.dm.SetPath(self.script_dir)

        # 关键修改2：显式加载字库文件
        self.font_file = os.path.join(self.script_dir, "game_font.txt")
        if os.path.exists(self.font_file):
            # 3.1232版本必须用SetDict加载字库文件
            ret = self.dm.SetDict(0, self.font_file)
            print(f"字库加载状态: {'成功' if ret == 1 else '失败'}")
        else:
            with open(self.font_file, "w", encoding="gbk") as f:
                f.write("[game_font]\n")
            print("新建空字库文件")

    def find_game_window(self, partial_title):

        try:
            for i in range(100):
                # 获取所有包含部分标题的窗口
                windows = pyautogui.getWindowsWithTitle(partial_title)

                if windows:
                    print(f"找到 {len(windows)} 个匹配窗口:")
                    for i, win in enumerate(windows, 1):
                        print(f" 完整标题: '{win.title}'")

                    # 返回第一个匹配的窗口
                    return windows[0]
                else:
                    print(f"未找到标题包含 '{partial_title}' 的窗口，重试第'{i + 1}'次...")
                    time.sleep(1)

            print(f"未找到标题包含 '{partial_title}' 的窗口")
            return None
        except Exception as e:
            print(f"查找窗口时出错: {e}")
        return None

    def running(self):
        # 查找游戏窗口
        game_window = self.find_game_window("Marvel")
        game_window.activate()

        pyautogui.keyDown('D')
        pyautogui.keyDown('W')
        time.sleep(15)
        pyautogui.keyUp('D')
        pyautogui.keyUp('W')

        time.sleep(20)

        pyautogui.keyDown('A')
        pyautogui.keyDown('W')
        time.sleep(1.5)
        pyautogui.keyUp('W')
        time.sleep(1)
        pyautogui.keyUp('A')



if __name__ == '__main__':
    dm = Running_12()
    dm.running()